/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.nxu.campus.zhxy.service.impl;

import com.nxu.campus.exception.EntityExistException;
import com.nxu.campus.utils.FileUtil;
import com.nxu.campus.utils.PageUtil;
import com.nxu.campus.utils.QueryHelp;
import com.nxu.campus.utils.ValidationUtil;
import com.nxu.campus.zhxy.domain.ZhxyMajor;
import com.nxu.campus.zhxy.repository.ZhxyMajorRepository;
import com.nxu.campus.zhxy.service.ZhxyMajorService;
import com.nxu.campus.zhxy.service.dto.ZhxyMajorDto;
import com.nxu.campus.zhxy.service.dto.ZhxyMajorQueryCriteria;
import com.nxu.campus.zhxy.service.mapstruct.ZhxyMajorMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HWL
* @date 2022-04-15
**/
@Service
@RequiredArgsConstructor
public class ZhxyMajorServiceImpl implements ZhxyMajorService {

    private final ZhxyMajorRepository zhxyMajorRepository;
    private final ZhxyMajorMapper zhxyMajorMapper;

    @Override
    public Map<String,Object> queryAll(ZhxyMajorQueryCriteria criteria, Pageable pageable){
        Page<ZhxyMajor> page = zhxyMajorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhxyMajorMapper::toDto));
    }

    @Override
    public List<ZhxyMajorDto> queryAll(ZhxyMajorQueryCriteria criteria){
        return zhxyMajorMapper.toDto(zhxyMajorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhxyMajorDto findById(Integer id) {
        ZhxyMajor zhxyMajor = zhxyMajorRepository.findById(id).orElseGet(ZhxyMajor::new);
        ValidationUtil.isNull(zhxyMajor.getId(),"ZhxyMajor","id",id);
        return zhxyMajorMapper.toDto(zhxyMajor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhxyMajorDto create(ZhxyMajor resources) {
        if(zhxyMajorRepository.findByName(resources.getName()) != null){
            throw new EntityExistException(ZhxyMajor.class,"major_name",resources.getName());
        }
        return zhxyMajorMapper.toDto(zhxyMajorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhxyMajor resources) {
        ZhxyMajor zhxyMajor = zhxyMajorRepository.findById(resources.getId()).orElseGet(ZhxyMajor::new);
        ValidationUtil.isNull( zhxyMajor.getId(),"ZhxyMajor","id",resources.getId());
        zhxyMajor = zhxyMajorRepository.findByName(resources.getName());
        if(zhxyMajor != null && !zhxyMajor.getId().equals(zhxyMajor.getId())){
            throw new EntityExistException(ZhxyMajor.class,"major_name",resources.getName());
        }
        zhxyMajor.copy(resources);
        zhxyMajorRepository.save(zhxyMajor);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            zhxyMajorRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhxyMajorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhxyMajorDto zhxyMajor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("专业代码", zhxyMajor.getMajorCode());
            map.put("专业名称", zhxyMajor.getName());
            map.put("开设部门", zhxyMajor.getDept().getName());
            map.put("学制", zhxyMajor.getAcademic());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
